//
// Created by zhuyi on 2021/9/19.
//
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

struct Review{
    string title;
    int rating;
};
bool operator<(const Review & r1,const Review & r2);
bool worseThan(const Review & r1,const Review & r2);
bool FillReview(Review & rr);
void ShowReview(const Review & rr);

int main(){
    vector<Review> books;
    Review temp;
    while (FillReview(temp)){
        books.push_back(temp);
    }
    if (books.size() > 0){
        cout << "Thank you .You entered the following: "<< books.size()<<"raings:\n"<<"Rating\tBook\n";
        for_each(books.begin(),books.end(), ShowReview);
        sort(books.begin(),books.end());
        cout <<"Sorted by title:\n"<<"Rating\tBook\n";
        for_each(books.begin(),books.end(), ShowReview);

        sort(books.begin(),books.end(), worseThan);
        cout <<"Sorted by rating:\n"<<"Rating\tBook\n";
        for_each(books.begin(),books.end(), ShowReview);

        random_shuffle(books.begin(),books.end());
        cout <<"After shuffling:\n"<<"Rating\tBook\n";
        for_each(books.begin(),books.end(), ShowReview);
    } else{
        cout <<"No entries.";
    }
    cout <<"Bye.\n";
    return 0;
}
bool operator<(const Review & r1,const Review & r2){
    if (r1.title < r2.title){
        return false;
    } else if (r1.title == r2.title && r1.rating < r2.rating){
        return true;
    }
    return false;
}
bool worseThan(const Review & r1,const Review & r2){
    if (r1.rating < r2.rating){
        return true;
    }
    return false;
}
bool FillReview(Review & rr){
    cout <<"Enter book title (quit to quit): ";
    getline(cin,rr.title);
    if (rr.title == "quit") return false;
    cout <<"Enter book rating: ";
    cin >> rr.rating;
    if (!cin){
        return false;
    }
    while (cin.get() != '\n'){
        continue;
    }
    return true;
}
void ShowReview(const Review & rr){
    cout << rr.rating <<"\t"<<rr.title << endl;
}